/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.schedule.executor.base;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

import com.hyts.schedule.executor.error.ScheduleError;

/** 
 * @title SimpleSchedulerFactory.java  
 * @package com.hyts.schedule.quartz  
 * @description TODO  
 * @see TODO
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年5月16日 
 * @version V1.0 
 */
public final class BaseSchedulerManager implements AbstractSchedulerManager{

	 /**  
	 * <p>任務調度器操作</p> 
	 * @fieldName scheduler
	 * @fieldType Scheduler
	 */ 
	private static Scheduler scheduler = null;
	
	//静态代码块
	static {
		synchronized(BaseSchedulerManager.class){
		 if(scheduler == null){
			 StdSchedulerFactory sf = new StdSchedulerFactory(); 
			 try {
				scheduler = sf.getScheduler();
			} catch (SchedulerException e) {
				e.printStackTrace();
			} 
		 }
		}
	}
	
	 /**
	 * @throws SchedulerException   
	 * <b>getInstance:获取</b>
	 * <p>工厂方法：获取任务调度器对象</p>
	 * @return
	 * @exception
	 */ 
	public static Scheduler getInstance(){
		 return scheduler;
	 }
	
	/* (非 Javadoc)
	 * @return
	 * @see com.hyts.schedule.quartz.base.AbstractSchedulerManager#start()  
	 * @exception
	 */ 
	@Override
	public boolean start() {
		 try {
			scheduler.start();
		} catch (SchedulerException e) {
			throw new ScheduleError(e);
		}
		return true;
	}

	/* (非 Javadoc)
	 * @return
	 * @see com.hyts.schedule.quartz.base.AbstractSchedulerManager#stop()  
	 * @exception
	 */ 
	@Override
	public boolean stop() {
		 try {
				scheduler.shutdown();;
		} catch (SchedulerException e) {
				throw new ScheduleError(e);
		}
		return true;
	}

	/* (非 Javadoc)
	 * @return
	 * @see com.hyts.schedule.quartz.base.AbstractSchedulerManager#isStarted()  
	 * @exception
	 */ 
	@Override
	public boolean isStarted() {
		try {
			return scheduler.isStarted();
		} catch (SchedulerException e) {
			throw new ScheduleError(e);
		}
	}

	/* (非 Javadoc)
	 * @return
	 * @see com.hyts.schedule.quartz.base.AbstractSchedulerManager#isStoped()  
	 * @exception
	 */ 
	@Override
	public boolean isStoped() {
		try {
			return scheduler.isShutdown();
		} catch (SchedulerException e) {
			throw new ScheduleError(e);
		}
	}

	/* (非 Javadoc)
	 * <b>TODO(方法功能的描述)-  start </b>
	 * <p>TODO(这里用一句话描述这个方法的作用)</p>
	 * @param delaySecond
	 * @return
	 * @see com.hyts.schedule.quartz.base.AbstractSchedulerManager#start(int)  
	 * @exception
	 */ 
	
	@Override
	public boolean start(int delaySecond) {
		 try {
				scheduler.startDelayed(delaySecond);
		} catch (SchedulerException e) {
				throw new ScheduleError(e);
		}
		return true;
	}

	/* (非 Javadoc)
	 * <b>TODO(方法功能的描述)-  stop </b>
	 * <p>TODO(这里用一句话描述这个方法的作用)</p>
	 * @param delaySecond
	 * @return
	 * @see com.hyts.schedule.quartz.base.AbstractSchedulerManager#stop(int)  
	 * @exception
	 */ 
	@Override
	public boolean stop(boolean isDelay) {
		 try {
			scheduler.shutdown(isDelay);
		} catch (SchedulerException e) {
				throw new ScheduleError(e);
		}
		return true;
	}
	
}
